import { defineStore } from 'pinia'
import {ref} from "vue";
import api from '../api/index.js'

export const useHomeStore = defineStore('home', () => {

    const word = ref({
        name: '',
        abs:''
    })
    const model = ref({
        left: '',
        right: '',
        num1: '',
        num2: ''
    })
    const concept = ref({
        name: '',
        concept: '',
        num: '',
    })
    const sword = ref('')
    const matchData = ref('')
    const wordData = ref('')
    const sWordData = ref('')
    const conceptData = ref('')
    const sentence = ref('')
    const output = ref('输出')
    function defSWord (){
        if (sword.value === '') {
            window.$message.error('请输入词组')
            return
        }
        api.add.sWord({name:sword.value,abs:''}).then(res => {
            window.$message.info(res.data)
        }).then(()=>{
            flash()
        })
    }

    function defWord() {
        if (word.value.name === '' || word.value.abs === '') {
            window.$message.error('词组或抽象不能为空')
            return
        }
        api.add.word(word.value).then((res)=>{
            window.$message.info(res.data)
        }).then(()=>{
            flash()
        })
    }

    function flash(){
        api.get.modelList().then(res => {
            matchData.value = res.data
        })
        api.get.wordList().then(res => {
            wordData.value = res.data
        })
        api.get.sWordList().then(res => {
            sWordData.value = res.data
        })
        api.get.conceptList().then(res => {
            conceptData.value = res.data
        })
    }
    function getMeaning () {
        if (sentence.value === '') {
            window.$message.error('请输入句子')
            return
        }
        api.get.meaning(sentence.value).then((res)=>{
            output.value = res.data
        })

    }
    function defModel () {
        if (model.value.left === '' || model.value.right === '' || model.value.num1 === '' || model.value.num2 === '') {
            window.$message.error('信息不能为空')
            return
        }
        api.add.match(model.value).then((res)=>{
            window.$message.info(res.data)
        }).then(()=>{
            flash()
        })
    }
    function defConcept () {
        if (concept.value.name === '' || concept.value.concept === '' || concept.value.num === '') {
            window.$message.error('信息不能为空')
            return
        }
        api.add.concept(concept.value).then((res)=>{
            window.$message.info(res.data)
        }).then(()=>{
            flash()
        })
    }


    return { word,sWordData, model, sword,conceptData,concept,defConcept, defSWord, defWord,matchData,wordData,flash,getMeaning,output,sentence,defModel }
})